Completed
Push — master ( a7453d...2ede20 )
by Sander
01:24
created

angular.controller(ꞌSettingsCtrlꞌ)   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 7
rs 9.4285
1
/* global API */
2
3
/**
4
 * Nextcloud - passman
5
 *
6
 * @copyright Copyright (c) 2016, Sander Brand ([email protected])
7
 * @copyright Copyright (c) 2016, Marcos Zuriaga Miguel ([email protected])
8
 * @license GNU AGPL version 3 or any later version
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License as
12
 * published by the Free Software Foundation, either version 3 of the
13
 * License, or (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU Affero General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public License
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
 *
23
 */
24
25
(function () {
26
    'use strict';
27
28
    /**
29
     * @ngdoc function
30
     * @name passmanApp.controller:MainCtrl
31
     * @description
32
     * # MainCtrl
33
     * Controller of the passmanApp
34
     */
35
    angular.module('passmanExtension')
36
        .controller('SettingsCtrl', ['$scope', '$rootScope', 'Settings', '$location', function ($scope, $rootScope, Settings, $location) {
0 ignored issues
show
Unused Code introduced by
The parameter $rootScope is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
Unused Code introduced by
The parameter $location is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
Unused Code introduced by
The parameter Settings is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
37
            $scope.settings = {
38
                nextcloud_host: '',
39
                nextcloud_username: '',
40
                nextcloud_password: '',
41
                ignoreProtocol: true,
42
                ignoreSubdomain: true,
43
                ignorePort: true,
44
                ignorePath: true,
45
                generatedPasswordLength: 12,
46
                remember_password: true,
47
                remember_vault_password: true,
48
                refreshTime: 60
49
            };
50
            $scope.errors = [];
51
52
            API.runtime.sendMessage(API.runtime.id, {'method': 'getRuntimeSettings'}).then(function (settings) {
53
                $scope.errors = [];
54
                if (settings) {
55
                    $scope.settings = angular.copy(settings);
56
                }
57
58
                $scope.get_vaults = function () {
59
                    if (!$scope.settings.hasOwnProperty('nextcloud_host') || !$scope.settings.hasOwnProperty('nextcloud_password') || !$scope.settings.hasOwnProperty('nextcloud_username')) {
60
                        return;
61
                    }
62
                    PAPI.username = $scope.settings.nextcloud_username;
63
                    PAPI.password = $scope.settings.nextcloud_password;
64
                    PAPI.host = $scope.settings.nextcloud_host;
65
66
                    PAPI.getVaults(function (vaults) {
67
                        $scope.errors = [];
68
                        var save_btn = jQuery('#save'),
69
                            login_required =  jQuery('.login-req');
70
                        if(vaults.hasOwnProperty('error')){
71
                            var errors = 'Invalid response from server: ['+ vaults.result.status +'] '+ vaults.result.statusText;
72
                            $scope.errors.push(errors);
73
                            login_required.hide();
74
                            save_btn.hide();
75
                            $scope.$apply();
76
                            return;
77
78
                        }
79
                        login_required.show();
80
                        save_btn.show();
81
                        $scope.vaults = vaults;
82
                        $scope.$apply();
83
84
                    });
85
                };
86
87
                $scope.$watch('[settings.nextcloud_host, settings.nextcloud_username, settings.nextcloud_password]', function(){
88
                    if ($scope.settings.nextcloud_host && $scope.settings.nextcloud_username && $scope.settings.nextcloud_password) {
89
                        $scope.get_vaults();
90
                    }
91
                }, true);
92
93
                if ($scope.settings.nextcloud_host && $scope.settings.nextcloud_username && $scope.settings.nextcloud_password) {
94
                    $scope.get_vaults();
95
                }
96
                $scope.$apply();
97
            });
98
99
            $scope.saving = false;
100
            $scope.saveSettings = function () {
101
                $scope.errors = [];
102
                var settings = angular.copy($scope.settings);
103
                try{
104
                    /** global: PAPI */
105
                    PAPI.decryptString(settings.default_vault.challenge_password, settings.vault_password);
106
                } catch (e){
107
                    $scope.errors.push('Invalid vault key!');
108
                    return;
109
                }
110
111
                $scope.saving = true;
112
                API.runtime.sendMessage(API.runtime.id, {method: "saveSettings", args: settings}).then(function () {
113
                    setTimeout(function () {
114
                        window.location = '#!/';
115
                        $scope.saving = false;
116
                    },750);
117
                });
118
            };
119
120
121
            $scope.cancel = function () {
122
                window.location = '#!/';
123
            };
124
        }]);
125
}());
126
127